<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Netty Zero-Copy：高性能网络编程的秘密武器</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 8px;
            overflow-x: auto;
            position: relative;
        }
        
        .code-block::before {
            content: 'Java';
            position: absolute;
            top: 8px;
            right: 12px;
            font-size: 12px;
            color: #666;
            background: #2d2d2d;
            padding: 2px 8px;
            border-radius: 4px;
        }
        
        .feature-card {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            transition: transform 0.3s ease;
        }
        
        .feature-card:hover {
            transform: scale(1.05);
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 8px;
            margin-top: -8px;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .highlight-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-weight: 600;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fade-in {
            animation: fadeInUp 0.8s ease-out;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center animate-fade-in">
            <h1 class="text-5xl md:text-6xl font-bold mb-6">
                Netty Zero-Copy
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">
                高性能网络编程的秘密武器
            </p>
            <div class="flex justify-center space-x-4 text-sm md:text-base">
                <span class="flex items-center">
                    <i class="fas fa-rocket mr-2"></i>
                    极致性能
                </span>
                <span class="flex items-center">
                    <i class="fas fa-memory mr-2"></i>
                    零拷贝技术
                </span>
                <span class="flex items-center">
                    <i class="fas fa-network-wired mr-2"></i>
                    网络优化
                </span>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-5xl mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="content-card rounded-xl p-8 mb-8 animate-fade-in">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>Java网络编程中，性能优化是一个永恒的话题。无论是处理高并发的Web服务器，还是构建低延迟的实时通信系统，如何高效地传输数据都是关键。Netty作为一款高性能的异步事件驱动网络框架，提供了许多优化技术，其中<span class="highlight-text">Zero-Copy（零拷贝）</span>是最为重要的特性之一。
            </p>
        </div>

        <!-- What is Zero-Copy -->
        <div class="content-card rounded-xl p-8 mb-8 animate-fade-in">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                什么是Zero-Copy？
            </h2>
            
            <h3 class="text-xl font-semibold mb-4 text-gray-700 flex items-center">
                <i class="fas fa-exclamation-triangle mr-3 text-yellow-500"></i>
                传统数据拷贝的问题
            </h3>
            
            <p class="text-gray-700 mb-6 leading-relaxed">
                在传统的网络编程中，数据从磁盘到网络接口的传输通常需要经过多次拷贝。以下是一个典型的文件传输流程：
            </p>
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-8">
                <div class="bg-gradient-to-r from-purple-50 to-pink-50 p-6 rounded-lg border-l-4 border-purple-500">
                    <div class="flex items-center mb-2">
                        <span class="text-2xl font-bold text-purple-600 mr-3">1</span>
                        <h4 class="font-semibold text-gray-800">磁盘到内核缓冲区</h4>
                    </div>
                    <p class="text-gray-600 text-sm">操作系统从磁盘读取文件数据到内核空间的缓冲区。</p>
                </div>
                
                <div class="bg-gradient-to-r from-blue-50 to-cyan-50 p-6 rounded-lg border-l-4 border-blue-500">
                    <div class="flex items-center mb-2">
                        <span class="text-2xl font-bold text-blue-600 mr-3">2</span>
                        <h4 class="font-semibold text-gray-800">内核缓冲区到用户缓冲区</h4>
                    </div>
                    <p class="text-gray-600 text-sm">数据从内核空间拷贝到用户空间的应用程序缓冲区。</p>
                </div>
                
                <div class="bg-gradient-to-r from-green-50 to-teal-50 p-6 rounded-lg border-l-4 border-green-500">
                    <div class="flex items-center mb-2">
                        <span class="text-2xl font-bold text-green-600 mr-3">3</span>
                        <h4 class="font-semibold text-gray-800">用户缓冲区到Socket缓冲区</h4>
                    </div>
                    <p class="text-gray-600 text-sm">应用程序将数据从用户空间拷贝到内核空间的Socket缓冲区。</p>
                </div>
                
                <div class="bg-gradient-to-r from-orange-50 to-red-50 p-6 rounded-lg border-l-4 border-orange-500">
                    <div class="flex items-center mb-2">
                        <span class="text-2xl font-bold text-orange-600 mr-3">4</span>
                        <h4 class="font-semibold text-gray-800">Socket缓冲区到网络接口</h4>
                    </div>
                    <p class="text-gray-600 text-sm">数据从Socket缓冲区发送到网络接口。</p>
                </div>
            </div>
            
            <div class="bg-red-50 border-l-4 border-red-400 p-6 rounded-lg mb-6">
                <p class="text-gray-700">
                    <i class="fas fa-info-circle text-red-500 mr-2"></i>
                    在这个过程中，数据被拷贝了<strong class="text-red-600 text-xl">4次</strong>，每次拷贝都会消耗CPU和内存资源，尤其是在传输大文件时，性能瓶颈尤为明显。
                </p>
            </div>
            
            <h3 class="text-xl font-semibold mb-4 text-gray-700 flex items-center">
                <i class="fas fa-lightbulb mr-3 text-yellow-400"></i>
                Zero-Copy的核心理念
            </h3>
            
            <p class="text-gray-700 leading-relaxed">
                Zero-Copy技术的目标是通过减少数据拷贝次数来提升性能。它允许数据直接从内核缓冲区传输到网络接口，而无需经过用户空间的中间拷贝。这样不仅可以减少CPU的开销，还能降低内存带宽的占用。
            </p>
        </div>

        <!-- Data Flow Visualization -->
        <div class="content-card rounded-xl p-8 mb-8 animate-fade-in">
            <h3 class="text-xl font-semibold mb-6 text-gray-700 text-center">
                <i class="fas fa-chart-line mr-2 text-blue-500"></i>
                传统拷贝 vs Zero-Copy 数据流对比
            </h3>
            <div class="mermaid">
                graph TB
                    subgraph "传统数据拷贝"
                        A1[磁盘] -->|拷贝1| B1[内核缓冲区]
                        B1 -->|拷贝2| C1[用户缓冲区]
                        C1 -->|拷贝3| D1[Socket缓冲区]
                        D1 -->|拷贝4| E1[网络接口]
                    end
                    
                    subgraph "Zero-Copy"
                        A2[磁盘] -->|DMA| B2[内核缓冲区]
                        B2 -->|直接传输| E2[网络接口]
                    end
                    
                    style A1 fill:#ff6b6b,stroke:#c92a2a,color:#fff
                    style E1 fill:#51cf66,stroke:#2f9e44,color:#fff
                    style A2 fill:#ff6b6b,stroke:#c92a2a,color:#fff
                    style E2 fill:#51cf66,stroke:#2f9e44,color:#fff
            </div>
        </div>

        <!-- Netty Implementation -->
        <div class="content-card rounded-xl p-8 mb-8 animate-fade-in">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                Netty中的Zero-Copy实现
            </h2>
            
            <p class="text-gray-700 mb-6 leading-relaxed">
                Netty通过多种方式实现了Zero-Copy技术，以下是几种常见的实现方式：
            </p>
            
            <!-- FileRegion -->
            <div class="mb-8">
                <h3 class="text-xl font-semibold mb-4 text-gray-700 flex items-center">
                    <i class="fas fa-file-code mr-3 text-purple-500"></i>
                    使用 <code class="bg-gray-100 px-2 py-1 rounded text-purple-600">FileRegion</code> 传输文件
                </h3>
                
                <p class="text-gray-700 mb-4">
                    <code class="bg-gray-100 px-2 py-1 rounded">FileRegion</code> 是Netty提供的一个接口，用于将文件内容直接传输到网络通道，而无需将文件数据加载到用户空间。
                </p>
                
                <div class="code-block p-6 mb-4">
                    <pre><code>File file = new File("largefile.zip");
FileInputStream fis = new FileInputStream(file);
FileChannel channel = fis.getChannel();
FileRegion region = new DefaultFileRegion(channel, 0, file.length());

ctx.writeAndFlush(region).addListener(future -> {
    if (future.isSuccess()) {
        System.out.println("File sent successfully!");
    } else {
        System.out.println("File send failed: " + future.cause());
    }
});</code></pre>
                </div>
                
                <p class="text-gray-700">
                    在这个例子